/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-6 下午1:39
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.Toastutils;

import org.xutils.common.Callback;
import org.xutils.common.util.MD5;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:修改密码
 * autour:guanluocang
 * date:17/7/6 13:39
 */
public class UpdataPasswordActivity extends BaseActivity {

    @Bind(R.id.et_oldPassword)//旧密码
            EditText et_oldPassword;
    @Bind(R.id.btn_hideOldPassword)
    Button btn_hideOldPassword;
    @Bind(R.id.et_newPassword)//新密码
            EditText et_newPassword;
    @Bind(R.id.btn_hideNewPassword)
    Button btn_hideNewPassword;
    @Bind(R.id.et_confimPassword)//确认密码
            EditText et_confimPassword;
    @Bind(R.id.btn_hideConfimPassword)
    Button btn_hideConfimPassword;
    private boolean isShow_old = true;
    private boolean isShow_new = true;
    private boolean isShow_confim = true;
    @Bind(R.id.tv_save)
    TextView tv_save;//保存


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_updata_password);
        initTitleBar(R.string.txt_setting_updatapassword);
        setRightTextVisibility(View.GONE);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        btn_hideOldPassword.setOnClickListener(this);
        btn_hideNewPassword.setOnClickListener(this);
        btn_hideConfimPassword.setOnClickListener(this);
        tv_save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_hideOldPassword:
                hideOrShowOldPass();
                break;
            case R.id.btn_hideNewPassword:
                hideOrShowNewPass();
                break;
            case R.id.btn_hideConfimPassword:
                hideOrShowConfimPass();
                break;
            case R.id.tv_save:
                doConfim();
                break;
        }
    }

    private void doConfim() {
        String oldPass = et_oldPassword.getText().toString().trim();
        String newPass = et_newPassword.getText().toString().trim();
        String confimPass = et_confimPassword.getText().toString().trim();

        String passOne_confim = MD5.md5(newPass);
        String passTwo_confim = MD5.md5(passOne_confim + passOne_confim);
        String passThree_confim = MD5.md5(passTwo_confim + "~!@#$%^&*()_+");

        String passOne = MD5.md5(confimPass);
        String passTwo = MD5.md5(passOne + passOne);
        String passThree = MD5.md5(passTwo + "~!@#$%^&*()_+");

        if (TextUtils.isEmpty(oldPass) || TextUtils.isEmpty(newPass) || TextUtils.isEmpty(confimPass)) {
            Toastutils.show("请输入");
        } else if (!newPass.equals(confimPass)) {
            Toastutils.show("输入不一致");
        } else {
            RequestParams params = new RequestParams(Config.updataPassword);
            params.addBodyParameter("id_auth", user.getId_auth());
            params.addBodyParameter("password_a", passThree_confim);
            params.addBodyParameter("password_b", passThree);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getErrMsg(result);
                    if (errCode == 200) {
                        Toastutils.show(errMsg);
                        finish();
                    }else{
                        Toastutils.show(errMsg);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }

    //密码的显示与隐藏
    public void hideOrShowOldPass() {
        if (isShow_old) {
            et_oldPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            btn_hideOldPassword.setBackgroundResource(R.drawable.ic_login_show);
        } else {
            et_oldPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            btn_hideOldPassword.setBackgroundResource(R.drawable.ic_login_hide);
        }
        isShow_old = !isShow_old;
        et_oldPassword.postInvalidate();
        //切换后将EditText光标置于末尾
        CharSequence charSequence_confirm = et_oldPassword.getText();
        if (charSequence_confirm instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence_confirm;
            Selection.setSelection(spanText, charSequence_confirm.length());
        }
    }

    //密码的显示与隐藏
    public void hideOrShowNewPass() {
        if (isShow_new) {
            et_newPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            btn_hideNewPassword.setBackgroundResource(R.drawable.ic_login_show);
        } else {
            et_newPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            btn_hideNewPassword.setBackgroundResource(R.drawable.ic_login_hide);
        }
        isShow_new = !isShow_new;
        et_newPassword.postInvalidate();
        //切换后将EditText光标置于末尾
        CharSequence charSequence_confirm = et_newPassword.getText();
        if (charSequence_confirm instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence_confirm;
            Selection.setSelection(spanText, charSequence_confirm.length());
        }
    }

    //密码的显示与隐藏
    public void hideOrShowConfimPass() {
        if (isShow_confim) {
            et_confimPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            btn_hideConfimPassword.setBackgroundResource(R.drawable.ic_login_show);
        } else {
            et_confimPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            btn_hideConfimPassword.setBackgroundResource(R.drawable.ic_login_hide);
        }
        isShow_confim = !isShow_confim;
        et_confimPassword.postInvalidate();
        //切换后将EditText光标置于末尾
        CharSequence charSequence_confirm = et_confimPassword.getText();
        if (charSequence_confirm instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence_confirm;
            Selection.setSelection(spanText, charSequence_confirm.length());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
